package com.gyul.findmystock.ui.stock.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gyul.findmystock.R;


public class VolumeChartView extends View {
	static final String TAG = "VolumeChartView";
	
	TextView [] textVolumes = new TextView[4];	
		
	float chartHeight;
	float chartHeightGap;		
	final int heightGapCount = 4;
	
	float convertFactor;	
	long maxData;
	
	Paint paintVolume;
		
	public VolumeChartView(Context context) {
		super(context);
		
		initializeChartView();
	}
	
	public VolumeChartView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		initializeChartView();
	}
	
	@Override
	public void onSizeChanged( int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		Log.d(TAG, "onSizeChanged");
		
		chartHeight = h;		
		chartHeightGap = chartHeight / heightGapCount;
				 
	}
	
	private void initializeChartView() {
		paintVolume = new Paint();
		paintVolume.setStyle(Paint.Style.FILL);
		paintVolume.setColor(Color.CYAN);
	}
	
	public void initializeChartInfo(View rootView) {
		this.textVolumes[0] = (TextView) rootView.findViewById(R.id.text_volume_value1);
		this.textVolumes[1] = (TextView) rootView.findViewById(R.id.text_volume_value2);
		this.textVolumes[2] = (TextView) rootView.findViewById(R.id.text_volume_value3);
		this.textVolumes[3] = (TextView) rootView.findViewById(R.id.text_volume_value4);		
	}
	
	@Override
	public void onDraw(Canvas canvas) {		
		if ( Chart.quoteList == null || Chart.initialized == false ) {
			return;
		}
		
		setFactor();		
		canvas.drawColor(Color.rgb(235, 235, 235));		
		drawVolume(canvas);
		drawRange();
		
		if ( Chart.showInfo ) {
			Chart.drawHighlightLine(canvas, chartHeight);
		}
	}
	
	private void drawVolume(Canvas canvas) {
		long volume = 0;
		float volumeX = 0;
		float left, right;
		
		for ( int i = Chart.startIndex; i < Chart.startIndex + Chart.drawStockSize && i < Chart.quoteListSize; i++ ) {
			volume = Chart.quoteList.get(i).volume;
//			Log.d(TAG, quoteList.get(i).date + " - " + volume);
			
			left = volumeX + Chart.chartWidthGapBlank;
			right = volumeX + Chart.chartWidthGap - Chart.chartWidthGapBlank;
			
			canvas.drawRect(left, getData(volume), right, chartHeight, paintVolume);
			
			volumeX += Chart.chartWidthGap;
		}
	}
	
	private void drawRange() {
		long gap = maxData / 4;
		long data = maxData;
		
		if ( textVolumes[0] == null ) {
			return;
		}
		
		for ( int i = 0; i < 4; i++ ) {
			textVolumes[i].setText("" + data);
			data -= gap;
		}
	}
	
	private void setFactor() {
		setMax();
		convertFactor = chartHeightGap / (maxData * 0.25f ); 
	}
	
	private float getData(long volume) {		
		return (maxData - volume) * convertFactor;
	}
	
	private void setMax() {
		maxData = Integer.MIN_VALUE;
		
		for ( int i = Chart.startIndex; i < Chart.startIndex + Chart.drawStockSize && i < Chart.quoteListSize; i++ ) {			
			maxData = Math.max(maxData, Chart.quoteList.get(i).volume);
		}
		
		if ( maxData % 10000 != 0 ) {
			maxData += 10000 - ( maxData % 10000);
		}
	}

}
